﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using LeapLibrary;
using Leap;

namespace TronLeapRacing
{
    class Control
    {
        //Game
        Game game1;
        GameTime gameTime;

        //Leap
        LeapComponet leap;

        //Leap Control
        Hand handOne, handTwo;
        Finger fingerLeft, fingerRight, fingerOne, fingerTwo;

        //Keyboard Control
        KeyboardState oldState;
        KeyboardState newState;

        public Vector3 coord;
        public float rotation;
        public float angle;
        bool ende = false;
        bool pause = false;

        //Sound for Turns
        SoundEffect turnSound;

        public Control(Game game1)
        {
            oldState = Keyboard.GetState();
            coord = new Vector3(0, 0, 0);
            rotation = 0;
            angle = 0;

            this.game1 = game1;

            //Leap Integration
            leap = new LeapComponet(game1);
            game1.Components.Add(leap);
        }

        public void LoadContent(ContentManager Content)
        {
            turnSound = Content.Load<SoundEffect>("Music/cycturn");
        }

        public void Initialize()
        {
            leap.Initialize();

            handOne = null;
            handTwo = null;
            fingerOne = null;
            fingerTwo = null;
            fingerLeft = null;
            fingerRight = null;
        }

        public void Update(GameTime gameTime)
        {
            this.gameTime = gameTime;
            newState = Keyboard.GetState();
            leap.Update(gameTime);

            //handOne = null;
            //handTwo = null;
            //fingerOne = null;
            //fingerTwo = null;
            //fingerLeft = null;
            //fingerRight = null;

            if (leap.leapFrame.Hands != null && leap.leapFrame.Hands.Count >= 2)
            {
                handOne = leap.leapFrame.Hands[0];
                handTwo = leap.leapFrame.Hands[1];

                if (handOne.Fingers != null && handOne.Fingers.Count >= 1)
                    fingerOne = handOne.Fingers[0];
                else
                    fingerOne = null;

                if (handTwo.Fingers != null && handTwo.Fingers.Count >= 1)
                    fingerTwo = handTwo.Fingers[0];
                else
                    fingerTwo = null;
            }
            else
            {
                handOne = null;
                handTwo = null;
                fingerOne = null;
                fingerTwo = null;
            }

            //init linker und rechter Finger, egal welcher erst erkannt wird
            if (thereAre2Fingers())
            {
                if (fingerOne.TipPosition.x < 0 && fingerTwo.TipPosition.x > 0)
                {
                    fingerLeft = fingerOne;
                    fingerRight = fingerTwo;
                }
                else if (fingerOne.TipPosition.x > 0 && fingerTwo.TipPosition.x < 0)
                {
                    fingerLeft = fingerTwo;
                    fingerRight = fingerOne;
                }
                else
                {
                    if (fingerOne.TipPosition.x < fingerTwo.TipPosition.x)
                    {
                        fingerLeft = fingerOne;
                        fingerRight = fingerTwo;
                    }
                    else
                    {
                        fingerLeft = fingerTwo;
                        fingerRight = fingerOne;
                    }
                }
            }
            else
            {
                fingerLeft = null;
                fingerRight = null;
            }
        }

        //true - wenn 2 Hände erkannt
        public Boolean thereAre2Hands()
        {
            return (handOne != null && handTwo != null);
        }

        //true - wenn 2 Finger erkannt
        public Boolean thereAre2Fingers()
        {
            return (fingerOne != null && fingerTwo != null);
        }

        //true - wenn das Bike beschleunigt
        public Boolean thereIsSpeed()
        {
            return (fingerOne.TipPosition.z < 0 || fingerTwo.TipPosition.z < 0);
        }

        public String colorLeftFingersDetection()
        {
            /*if (leap.leapFrame.Hands.Count == 1)
            {
                if (leap.leapFrame.Hands[0].Fingers.Count >= 1)
                {
                    if (leap.leapFrame.Hands[0].Fingers[0].TipPosition.x < 0 && leap.leapFrame.Hands[0].Fingers[0].TipPosition.x > -180)
                        return "Texturen/blueLeft";
                    else if (leap.leapFrame.Hands[0].Fingers[0].TipPosition.x < 0 && leap.leapFrame.Hands[0].Fingers[0].TipPosition.x < -180)
                        return "Texturen/orangeLeft";
                    else
                        return "Texturen/redLeft";
                }
                else
                    return "Texturen/redLeft";
            }
            else
            {*/
                if (fingerLeft != null)
                {
                    if (fingerLeft.TipPosition.x > -180 && fingerLeft.TipPosition.x < 0)
                        return "Texturen/blueLeft";
                    else
                        return "Texturen/orangeLeft";
                }
                else
                    return "Texturen/redLeft";
            //}
        }

        public String colorRightFingersDetections()
        {
            /*if (leap.leapFrame.Hands.Count == 1)
            {
                if (leap.leapFrame.Hands[0].Fingers.Count >= 1)
                {
                    if (leap.leapFrame.Hands[0].Fingers[0].TipPosition.x > 0 && leap.leapFrame.Hands[0].Fingers[0].TipPosition.x < 180)
                        return "Texturen/blueRight";
                    else if (leap.leapFrame.Hands[0].Fingers[0].TipPosition.x > 0 && leap.leapFrame.Hands[0].Fingers[0].TipPosition.x > 180)
                        return "Texturen/orangeRight";
                    else
                        return "Texturen/redRight";
                }
                else
                    return "Texturen/redRight";
            }
            else
            {*/
                if (fingerRight != null)
                {
                    if (fingerRight.TipPosition.x < 180 && fingerRight.TipPosition.x > 0)
                        return "Texturen/blueRight";
                    else
                        return "Texturen/orangeRight";
                }
                else
                    return "Texturen/redRight";
            //}
        }

        //Bewegungssteuerung
        public void move()
        {
            if (newState.IsKeyDown(Keys.A) && coord.Z >= -17f)
            {
                if (angle > 0)
                    angle -= 0.1f;
                else
                {
                    coord.Z -= 0.2f;
                    if (angle > -0.8)
                        angle -= 0.01f;
                    Console.WriteLine("left");
                }
            }
            if (newState.IsKeyDown(Keys.D) && coord.Z <= 17f)
            {
                if (angle < 0)
                    angle += 0.1f;
                else
                {
                    coord.Z += 0.2f;
                    if (angle < 0.8f)
                        angle += 0.01f;
                    Console.WriteLine("right");
                }
            }

            if (newState.IsKeyDown(Keys.W))
            {
                coord.X += 0.5f;
                rotation += 0.2f;
            }

            if (newState.IsKeyDown(Keys.S))
            {
                rotation -= 0.1f;
                coord.X -= 0.1f;
            }

            //LEAP STEUERUNG
            
            if (thereAre2Fingers())
            {
                float differenz = fingerLeft.TipPosition.z - fingerRight.TipPosition.z;
                if (differenz < 0) differenz *= -1;

                //lenken
                if (fingerLeft.TipPosition.z < fingerRight.TipPosition.z && differenz > 40)
                {
                    if (coord.Z <= 17f)
                        coord.Z += 0.5f;

                    if (angle <= 0.4f)
                        angle += 0.1f;
                }
                else if (fingerRight.TipPosition.z < fingerLeft.TipPosition.z && differenz > 40)
                {
                    if (coord.Z >= -17f)
                        coord.Z -= 0.5f;

                    if (angle >= -0.4f)
                        angle -= 0.1f;
                }
                else
                {
                    angle = 0;
                }

                //beschleunigung - FixWert wenn ein Finger vertikal hinter der Leap
                if (fingerLeft.TipPosition.z < 0 || fingerRight.TipPosition.z < 0)
                {
                    coord.X += 1.0f;
                    rotation += 0.2f;
                }
            }
            else
            {
                fingerLeft = null;
                fingerRight = null;
            }   
        }

        public bool isPaused()
        {
            if (!ende)
            {
                if (newState.IsKeyDown(Keys.P))
                    return true;
                return false;
            }
            else
            {
                return false;
            }
        }

        public Vector2 getPosition()
        {
            float x = 0;
            float y = 0;
            if (leap.FirstHand != null)
            {
                x = leap.FirstHand.PalmPosition.x;
                y = leap.FirstHand.PalmPosition.y;
            }
            return new Vector2(x, y);
        }

        public bool isSwipe()
        {
            leap.Update(gameTime);
            for (int i = 0; i < leap.Gestures.Count(); i++)
            {
                SwipeGesture swipe = new SwipeGesture(leap.Gestures[i]);
                if (swipe.Speed > 2000)
                    return true;
            }
            return false;

        }

        public String optionsSwipe()
        {
            leap.Update(gameTime);
            for (int i = 0; i < leap.Gestures.Count(); i++)
            {
                SwipeGesture swipe = new SwipeGesture(leap.Gestures[i]);
                if (swipe.Position.x - swipe.StartPosition.x > 50)
                    return "right";
                else if (swipe.Position.x - swipe.StartPosition.x < -50)
                    return "left";
            }
            return "null";
        }

        public void setEnde(bool ende)
        {
            this.ende = ende;
        }

        public void setPause(bool pause)
        {
            this.pause = pause;
        }

        public void Draw(GameTime gameTime)
        {
            leap.Draw(gameTime);
        }

    }
}
